<template>
  <view class="container">
    <view class="header">
      <view class="title">速判选择设置</view>
      <view class="subtitle">为分数选择正确的估算值</view>
    </view>

    <view class="settings-container">
      <!-- 题目数量设置 -->
      <view class="setting-item">
        <view class="setting-label">
          <uni-icons color="#007AFF" size="20" type="list"></uni-icons>
          <text>题目数量</text>
        </view>
        <view class="setting-value">
          <uni-number-box
              :max="20"
              :min="5"
              :value="questionCount"
              @change="onQuestionCountChange"
          ></uni-number-box>
        </view>
      </view>

      <!-- 选项数量设置 -->
      <view class="setting-item">
        <view class="setting-label">
          <uni-icons color="#007AFF" size="20" type="bars"></uni-icons>
          <text>每题选项数</text>
        </view>
        <view class="setting-value">
          <uni-number-box
              :max="6"
              :min="3"
              :value="optionsCount"
              @change="onOptionsCountChange"
          ></uni-number-box>
        </view>
      </view>

      <!-- 练习说明 -->
      <view class="info-card">
        <view class="info-title">
          <uni-icons color="#28a745" size="20" type="info"></uni-icons>
          <text>练习说明</text>
        </view>
        <view class="info-content">
          <text class="info-text">• 为每个分数选择正确的小数估算值</text>
          <text class="info-text">• 分数范围：1/20 至 1/2</text>
          <text class="info-text">• 在多个选项中选择最接近的答案</text>
          <text class="info-text">• 完成所有题目后提交答案</text>
        </view>
      </view>

      <!-- 历史最佳 -->
      <view v-if="bestScore !== null" class="stats-card">
        <view class="stats-title">历史最佳</view>
        <view class="stats-content">
          <view class="stat-item">
            <text class="stat-value">{{ bestScore }}分</text>
            <text class="stat-label">最高分数</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ bestAccuracy }}%</text>
            <text class="stat-label">最高正确率</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 开始按钮 -->
    <view class="start-container">
      <button class="start-btn" @tap="startPractice">
        <uni-icons color="#ffffff" size="20" type="right"></uni-icons>
        <text>开始练习</text>
      </button>
    </view>
  </view>
</template>

<script>
import Storage from '@/utils/storage.js';

export default {
  data() {
    return {
      questionCount: 10,
      optionsCount: 4,
      bestScore: null,
      bestAccuracy: null
    }
  },
  onLoad() {
    this.loadSettings();
    this.loadBestRecord();
  },
  methods: {
    // 题目数量变化
    onQuestionCountChange(value) {
      this.questionCount = value;
      this.saveSettings();
    },

    // 选项数量变化
    onOptionsCountChange(value) {
      this.optionsCount = value;
      this.saveSettings();
    },

    // 开始练习
    startPractice() {
      uni.navigateTo({
        url: `/pages/speed-judge-choice/practice?count=${this.questionCount}&options=${this.optionsCount}`
      });
    },

    // 加载设置
    loadSettings() {
      const settings = Storage.getUserSettings();
      this.questionCount = settings.speedJudgeChoiceQuestionCount || 10;
      this.optionsCount = settings.speedJudgeChoiceOptionsCount || 4;
    },

    // 保存设置
    saveSettings() {
      Storage.saveUserSettings({
        speedJudgeChoiceQuestionCount: this.questionCount,
        speedJudgeChoiceOptionsCount: this.optionsCount
      });
    },

    // 加载最佳记录
    loadBestRecord() {
      try {
        const records = Storage.get('speed_judge_choice_records', []);
        if (records.length > 0) {
          const best = records.reduce((max, record) =>
              record.score > max.score ? record : max
          );
          this.bestScore = best.score;
          this.bestAccuracy = best.accuracy;
        }
      } catch (e) {
        console.log('Failed to load best record:', e);
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

.header {
  background: linear-gradient(135deg, #ff6b6b, #ee5a52);
  padding: 60rpx 40rpx 40rpx;
  color: white;
  text-align: center;

  .title {
    font-size: 40rpx;
    font-weight: bold;
    margin-bottom: 15rpx;
  }

  .subtitle {
    font-size: 26rpx;
    opacity: 0.9;
  }
}

.settings-container {
  padding: 40rpx;
}

.setting-item {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .setting-label {
    display: flex;
    align-items: center;

    text {
      font-size: 30rpx;
      font-weight: 500;
      margin-left: 15rpx;
      color: #333;
    }
  }
}

.info-card {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .info-title {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;

    text {
      font-size: 30rpx;
      font-weight: 500;
      margin-left: 15rpx;
      color: #333;
    }
  }

  .info-content {
    .info-text {
      display: block;
      font-size: 26rpx;
      color: #666;
      line-height: 40rpx;
      margin-bottom: 10rpx;
    }
  }
}

.stats-card {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .stats-title {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 25rpx;
    text-align: center;
  }

  .stats-content {
    display: flex;
    justify-content: space-around;
  }

  .stat-item {
    text-align: center;

    .stat-value {
      display: block;
      font-size: 32rpx;
      font-weight: bold;
      color: #ff6b6b;
      margin-bottom: 8rpx;
    }

    .stat-label {
      font-size: 24rpx;
      color: #666;
    }
  }
}

.start-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx 40rpx;
  background: white;
  box-shadow: 0 -5rpx 15rpx rgba(0, 0, 0, 0.1);
}

.start-btn {
  width: 100%;
  height: 90rpx;
  background: linear-gradient(135deg, #ff6b6b, #ee5a52);
  border: none;
  border-radius: 45rpx;
  color: white;
  font-size: 32rpx;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 20rpx rgba(255, 107, 107, 0.3);

  text {
    margin-left: 10rpx;
  }

  &:active {
    transform: scale(0.98);
  }
}
</style>

